<template>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="登录账号：" prop="name">
      {{ruleForm.usercode}}
    </el-form-item>
    <el-form-item label="姓名：" prop="name" >
      <el-col :span="6">
        <el-input v-model.number="ruleForm.name" :disabled="true"></el-input>
      </el-col>
    </el-form-item>
    <el-form-item label="新密码：" prop="pass">
      <el-col :span="6">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-col>
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPass">
      <el-col :span="6">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-col>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import axios from 'axios';
  export default {
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          noid:'',
          usercode:'',
          name: '',
          oldpass: '',
          pass:'',
          checkPass: '',
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('是否确认修改？');
            let param={
              noid:this.ruleForm.noid,
              userPassword:this.$md5(this.ruleForm.checkPass,'a1b2c3defg'),
            };
            axios.post("/chain-api/userlogin/editPassword",param).then(
              res=>{
                console.log(res);
                if(res.data.code==1){
                  this.$message({
                    showClose: true,
                    message: "修改成功",
                    type: 'success'
                  })
                  this.$router.push("/login");
                }else{
                  this.$message.error({
                    showClose: true,
                    message:"修改失败",
                  })
                }
              }
            )
          } else {
            console.log('是否取消修改!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    mounted() {
      this.ruleForm.noid=this.$store.state.loginuserinfo.noid;
      this.ruleForm.usercode=this.$store.state.loginuserinfo.userCode;//账号
      this.ruleForm.name=this.$store.state.loginuserinfo.userName;//用户名
      this.ruleForm.oldpass=this.$store.state.loginuserinfo.userPassword;//密码
    },
  }
</script>

<style scoped>
/deep/ .el-form-item__label{
    color: #fff;
}
</style>